<!-- 列表的每一项 -->
<template>
  <div
    class="item-wrap"
    :class="{ actived: isActived }"
    @mouseenter="mousehandler('enter')"
    @mouseleave="mousehandler('leave')"
  >
    <div class="flex">
      <div class="cirlce"></div>
      <div class="content">{{ item.content }}</div>
    </div>
    <div class="flex">
      <div v-if="isActived" class="btn" @click="handleRemove(item.id)">
        删除
      </div>
      <input type="checkbox" v-model="purchased" />
    </div>
  </div>
</template>

<script lang="ts">
import { computed, defineComponent, PropType, ref } from 'vue'
import { Item } from '@/types/item'

export default defineComponent({
  name: 'ToDoItem',
  props: {
    item: {
      type: Object as () => Item, // 固定写法，表示类型为接口 Item 的对象
      required: true
    },
    removeItem: {
      type: Function as PropType<(id: number) => void>,
      required: true
    }
  },
  setup(props, { emit }) {
    const isActived = ref(false)
    function mousehandler(flag: string) {
      isActived.value = flag === 'enter' ? true : false
    }
    function handleRemove(id: number) {
      props.removeItem(id)
    }
    // 切换勾选框状态
    const purchased = computed({
      get() {
        return props.item.purchased
      },
      set(newVal) {
        emit('change-checked', props.item, newVal)
      }
    })
    return { mousehandler, handleRemove, isActived, purchased }
  }
})
</script>

<style scoped lang="scss">
.item-wrap {
  padding: 20px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  &:not(:last-child) {
    border-bottom: 1px solid #fff;
  }
}
.cirlce {
  width: 12px;
  height: 12px;
  background-color: #fcb7a0;
  border-radius: 6px;
  margin-right: 8px;
}
.content {
  color: #fcb7a0;
  font-weight: bold;
}
.flex {
  display: flex;
  align-items: center;
}
.btn {
  padding: 0 20px;
  font-size: 14px;
  color: #fc541dd2;
}
.actived {
  background-color: rgba($color: #ffffff, $alpha: 0.6);
}
</style>
